import type { ConfigItem } from '@/types/tester';
import { handleResult } from './handle-result';
import { formateDateTimeNew, formatUnit, formatNumber2String } from './format';
import { unitMap } from '@/config';

// 断路器断口类型
const breakTypeMap = {
  '0x00': '金属断口',
  '0x01': '合闸电阻',
  '0x02': '石墨触头',
};

// 测试类型
const testTypeMap = {
  '0x01': '分闸测试',
  '0x02': '合闸测试',
  '0x03': '分合测试',
  '0x04': '合分测试',
  '0x05': '分合分测试',
};

// 线圈类型
const lineTypeMap = {
  '0x01': '分闸线圈1',
  '0x02': '分闸线圈2',
  '0x03': '合闸线圈',
  '0x55': '本次未测断口',
};

/**
 * A1-C2 的重复配置
 * @returns ConfigItem[]
 */
const repeatConfig = (): ConfigItem[] => {
  // 进行重复的数组
  const arrNew = ['A1', 'B1', 'C1', 'A2', 'B2', 'C2'];
  // 服务器端的 key
  const serverKey = ['aOne', 'bOne', 'cOne', 'aTwo', 'bTwo', 'cTwo'];
  // 每组数据的长度
  const len = [1, 4, 4, 1, 4, 4, 2, 4, 4, 4];

  const result: ConfigItem[][] = arrNew.map((item, index) => {
    // 每组数据的起始位置
    const start = 13 + index * 32;
    const reduceLenFunc = (l: number) => {
      const _len = len.slice(0, l);
      return start + _len.reduce((total, item) => total + item, 0);
    };
    return [
      {
        label: `${item}断口线圈类型`,
        key: `${item}LineType`,
        start,
        length: len[0],
        dataType: 'int8',
        format: value => formatUnit(value, lineTypeMap),
        serverKey: `${serverKey[index]}BreakCoilType`,
      },
      {
        label: `${item}断口动作时间`,
        key: `${item}BreakTime`,
        start: reduceLenFunc(1),
        length: len[1],
        dataType: 'float',
        unit: unitMap['0x29'],
        serverKey: `${serverKey[index]}BreakActionTime`,
        precision: 2,
        serverFormat: (value) => formatNumber2String(value as number),
      },
      {
        label: `${item}弹跳时间`,
        key: `${item}BounceTime`,
        start: reduceLenFunc(2),
        length: len[2],
        dataType: 'float',
        unit: unitMap['0x29'],
        serverKey: `${serverKey[index]}BounceTime`,
        precision: 2,
        serverFormat: (value) => formatNumber2String(value as number),
      },
      {
        label: `${item}弹跳次数`,
        key: `${item}BounceCount`,
        start: reduceLenFunc(3),
        length: len[3],
        dataType: 'int8',
        serverKey: `${serverKey[index]}BounceFrequency`,
        serverFormat: (value) => formatNumber2String(value as number),
      },
      {
        label: `${item}断口金属短路时间`,
        key: `${item}ShortTime`,
        start: reduceLenFunc(4),
        length: len[4],
        dataType: 'float',
        unit: unitMap['0x29'],
        serverKey: `${serverKey[index]}BreakScTime`,
        precision: 2,
        serverFormat: (value) => formatNumber2String(value as number),
      },
      {
        label: `${item}断口无电流时间`,
        key: `${item}NoCurrentTime`,
        start: reduceLenFunc(5),
        length: len[5],
        dataType: 'float',
        unit: unitMap['0x29'],
        serverKey: `${serverKey[index]}BreakNcTime`,
        precision: 2,
        serverFormat: (value) => formatNumber2String(value as number),
      },
      {
        label: `${item}断口合闸电阻`,
        key: `${item}BreakResist`,
        start: reduceLenFunc(6),
        length: len[6],
        dataType: 'int16',
        serverKey: `${serverKey[index]}BreakCrv`,
        serverUnit: unitMap['0x1A'],
        serverFormat: (value) => formatNumber2String(value as number),
      },
      {
        label: `${item}断口合闸电阻投切时间`,
        key: `${item}BreakResistTime`,
        start: reduceLenFunc(7),
        length: len[7],
        dataType: 'float',
        unit: unitMap['0x29'],
        serverKey: `${serverKey[index]}BreakCriTime`,
        precision: 2,
        serverFormat: (value) => formatNumber2String(value as number),
      },
      {
        label: `${item}断口速度`,
        key: `${item}BreakSpeed`,
        start: reduceLenFunc(8),
        length: len[8],
        dataType: 'float',
        unit: unitMap['0x19'],
        serverKey: `${serverKey[index]}BreakSpeed`,
        precision: 2,
        serverFormat: (value) => formatNumber2String(value as number),
      },
      {
        label: `${item}断口超程`,
        key: `${item}BreakDistance`,
        start: reduceLenFunc(9),
        length: len[9],
        dataType: 'float',
        unit: unitMap['0x10'],
        precision: 2,
        serverKey: `${serverKey[index]}BreakOvertravel`,
        serverFormat: (value) => formatNumber2String(value as number),
      },
    ];
  });
  return result.flat();
};

// 线圈电流波形采样点数 N
const N = (): ConfigItem => {
  return {
    label: '线圈电流波形采样点数 N',
    key: 'N',
    start: 233,
    length: 4,
    dataType: 'int32',
    serverKey: 'coilCurrentWaveCount',
    serverFormat: (value) => formatNumber2String(value as number),
  };
};

// 基于 N 计算出来的数据
const basedOnNConfig = (): ConfigItem[] => {
  const nValue = handleResult('', [N()]);
  const len = nValue.length ? Number(nValue[0].value) : 0;
  return [
    {
      label: `A 相线圈电流波形`,
      key: 'AWaveform',
      start: 242,
      length: 2 * len,
      dataType: 'int16',
      format: value => {
        if (Number.isNaN(value)) return '-';
        if (value === Infinity) return '-';
        return `${value}${unitMap['0x0A']}`;
      },
      serverKey: 'aCoilCurrentWaveform',
    },
    {
      label: `B 相线圈电流波形`,
      key: 'BWaveform',
      start: 242 + 2 * len,
      length: 2 * len,
      dataType: 'int16',
      format: value => {
        if (Number.isNaN(value)) return '-';
        if (value === Infinity) return '-';
        return `${value}${unitMap['0x0A']}`;
      },
      serverKey: 'bCoilCurrentWaveform',
    },
    {
      label: `C 相线圈电流波形`,
      key: 'CWaveform',
      start: 242 + 4 * len,
      length: 2 * len,
      dataType: 'int16',
      format: value => {
        if (Number.isNaN(value)) return '-';
        if (value === Infinity) return '-';
        return `${value}${unitMap['0x0A']}`;
      },
      serverKey: 'cCoilCurrentWaveform',
    },
    // {
    //   label: 'A 相储能电流',
    //   key: 'aEnergyCurrent',
    //   start: 242 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x09'],
    //   serverKey: 'aChargingCurrent',
    // },
    // {
    //   label: 'B 相储能电流',
    //   key: 'bEnergyCurrent',
    //   start: 246 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x09'],
    //   serverKey: 'bChargingCurrent',
    // },
    // {
    //   label: 'C 相储能电流',
    //   key: 'cEnergyCurrent',
    //   start: 250 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x09'],
    //   serverKey: 'cChargingCurrent',
    // },
    // {
    //   label: 'A 相储能时间',
    //   key: 'aEnergyTime',
    //   start: 254 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x28'],
    //   serverKey: 'aChargingTime',
    // },
    // {
    //   label: 'B 相储能时间',
    //   key: 'bEnergyTime',
    //   start: 258 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x28'],
    //   serverKey: 'bChargingTime',
    // },
    // {
    //   label: 'C 相储能时间',
    //   key: 'cEnergyTime',
    //   start: 262 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x28'],
    //   serverKey: 'cChargingTime',
    // },
    // {
    //   label: 'A 相合闸动作电压',
    //   key: 'aBreakVoltage',
    //   start: 266 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x05'],
    //   serverKey: 'aClosingPickupVoltage',
    // },
    // {
    //   label: 'B 相合闸动作电压',
    //   key: 'bBreakVoltage',
    //   start: 270 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x05'],
    //   serverKey: 'bClosingPickupVoltage',
    // },
    // {
    //   label: 'C 相合闸动作电压',
    //   key: 'cBreakVoltage',
    //   start: 274 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x05'],
    //   serverKey: 'cClosingPickupVoltage',
    // },
    // {
    //   label: 'A 相分闸动作电压',
    //   key: 'aOpenVoltage',
    //   start: 278 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x05'],
    //   serverKey: 'aTrippingPickupVoltage',
    // },
    // {
    //   label: 'B 相分闸动作电压',
    //   key: 'bOpenVoltage',
    //   start: 282 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x05'],
    //   serverKey: 'bTrippingPickupVoltage',
    // },
    // {
    //   label: 'C 相分闸动作电压',
    //   key: 'cOpenVoltage',
    //   start: 286 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x05'],
    //   serverKey: 'cTrippingPickupVoltage',
    // },
    // {
    //   label: 'A 相分闸反弹幅值',
    //   key: 'aRebound',
    //   start: 290 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x10'],
    //   serverKey: 'aTripReboundAmplitude',
    // },
    // {
    //   label: 'B 相分闸反弹幅值',
    //   key: 'bRebound',
    //   start: 294 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x10'],
    //   serverKey: 'bTripReboundAmplitude',
    // },
    // {
    //   label: 'C 相分闸反弹幅值',
    //   key: 'cRebound',
    //   start: 298 + 6 * len,
    //   length: 4,
    //   dataType: 'float',
    //   unit: unitMap['0x10'],
    //   serverKey: 'cTripReboundAmplitude',
    // },
  ];
};

// 获取 N 的值

export const mechanicalConfig: ConfigItem[] = [
  {
    label: '测试时间',
    key: 'datetime',
    start: 0,
    length: 7,
    dataType: 'unit8',
    format: value => formateDateTimeNew(value),
    serverKey: 'monitorTime',
  },
  {
    label: '环境温度',
    key: 'ambientTemperature',
    start: 0,
    length: 0,
    dataType: 'char',
    serverKey: 'ambientTemperature',
    editable: true,
    editType: 'input',
    unit: unitMap['0x11'],
  },
  {
    label: '空气湿度',
    key: 'airHumidity',
    start: 0,
    length: 0,
    dataType: 'char',
    serverKey: 'airHumidity',
    editable: true,
    editType: 'input',
    unit: unitMap['0x08'],
  },
  {
    label: '备注',
    key: 'remark',
    start: 0,
    length: 0,
    dataType: 'char',
    serverKey: 'remark',
    editable: true,
    editType: 'input',
  },
  {
    label: '预警提示',
    key: 'warnPrompt',
    start: 0,
    length: 0,
    dataType: 'char',
    serverKey: 'warnPrompt',
    hide: true,
    hideInHistory: false,
  },
  {
    label: '断口类型',
    key: 'breakType',
    start: 7,
    length: 1,
    dataType: 'int8',
    format: value => formatUnit(value, breakTypeMap),
    serverKey: 'circuitBreakType',
  },
  {
    label: '开关行程',
    key: 'switchDistance',
    start: 8,
    length: 4,
    dataType: 'float',
    unit: unitMap['0x10'],
    serverKey: 'operationSchedule',
    precision: 2,
    serverFormat: (value) => formatNumber2String(value as number),
  },
  {
    label: '测试类型',
    key: 'testType',
    start: 12,
    length: 1,
    dataType: 'int8',
    format: value => formatUnit(value, testTypeMap),
    serverKey: 'testType',
  },
  ...repeatConfig(),
  {
    label: 'A相线圈电流',
    key: 'aCurrent',
    start: 205,
    length: 4,
    dataType: 'float',
    unit: unitMap['0x09'],
    serverKey: 'aCoilCurrent',
    serverFormat: (value) => formatNumber2String(value as number),
  },
  {
    label: 'B相线圈电流',
    key: 'bCurrent',
    start: 209,
    length: 4,
    dataType: 'float',
    unit: unitMap['0x09'],
    serverKey: 'bCoilCurrent',
    serverFormat: (value) => formatNumber2String(value as number),
  },
  {
    label: 'C相线圈电流',
    key: 'cCurrent',
    start: 213,
    length: 4,
    dataType: 'float',
    unit: unitMap['0x09'],
    serverKey: 'cCoilCurrent',
    serverFormat: (value) => formatNumber2String(value as number),
  },
  {
    label: 'A相同相同期',
    key: 'aPeriod',
    start: 217,
    length: 4,
    dataType: 'float',
    unit: unitMap['0x29'],
    serverKey: 'aPSynchronization',
    serverFormat: (value) => formatNumber2String(value as number),
  },
  {
    label: 'B相同相同期',
    key: 'bPeriod',
    start: 221,
    length: 4,
    dataType: 'float',
    unit: unitMap['0x29'],
    serverKey: 'bPSynchronization',
    serverFormat: (value) => formatNumber2String(value as number),
  },
  {
    label: 'C相同相同期',
    key: 'cPeriod',
    start: 225,
    length: 4,
    dataType: 'float',
    unit: unitMap['0x29'],
    serverKey: 'cPSynchronization',
    serverFormat: (value) => formatNumber2String(value as number),
  },
  {
    label: '相间同期',
    key: 'phasePeriod',
    start: 229,
    length: 4,
    dataType: 'float',
    unit: unitMap['0x29'],
    serverKey: 'phaseSynchronization',
    serverFormat: (value) => formatNumber2String(value as number),
  },
  N(),
  {
    label: '时间步长',
    key: 'timeStep',
    start: 237,
    length: 4,
    dataType: 'float',
    precision: 3,
    readUnit: 'timeStepUnit',
    serverKey: 'samplingTimeStep',
    serverFormat: (value) => formatNumber2String(value as number),
  },
  {
    label: '时间步长单位',
    key: 'timeStepUnit',
    start: 241,
    length: 1,
    dataType: 'unit8',
    format: formatUnit,
    serverKey: 'timeStep',
    hide: true,
  },
  ...basedOnNConfig(),
];
